<template>
  <div class style="width: 600px; height: 600px">
    
    <!-- <ts-title-panel title="3333" titleBackgroundColor="red" :data="{borderColor:'red'}"></ts-title-panel> -->
    <ts-table :source="source" height="100%" ref="tstable"></ts-table>
  </div>
</template>

<script>
// import tsG2Chart from '@/components/tsG2Chart.vue'
export default {
  name: 'treetable',
  components: {},
  data: function() {
    return {
      source: this.sourceFn()
    }
  },
  created() {},
  mounted() {},
  methods: {
    loadChildrenMethod({ row }) {
      let _this = this
      let param = {
        busiCode: '80002', //指标code只能单个，必填
        beginDate: '2020-05-01', //开始时间，必填
        endDate: '2020-05-02', //结束时间，非必填，默认beginDate的值
        orgIds: '01,02' //机构，支持多个，非必填}
      }
      return new Promise(resolve => {
        let url = ''
        if (!row.level || row.level == 0) {
          //
          // 时间，进入第二级
          row.level = 0
          url = '/ts-bi/api/v3/busi/dept/month/statistic'
          let year = param.beginDate.substring(0, 4)
          let month = row.category.substring(0, 2)
          param.beginDate = year + '-' + month + '-01'
          let days = new Date(year, month, 0).getDate()
          param.endDate = year + '-' + month + days
        } else if ((row.level = 1)) {
          // 进入第三级
          url = '/ts-bi/api/v3/busi/doct/month/statistic'
          param = row.param
          param.deptCode = row.deptCode
        }
        this.$api({
          url: url,
          data: param,
          method: 'post',
          headers: {
            'Content-Type': 'application/json; charset=utf-8'
          }
        })
          .then(res => {
            if (res.success) {
              let data = res.object || res.rows
              data.forEach(re => {
                re.level = row.level + 1 // 层级加1
                if (row.level == 0) {
                  re.category = re.deptName
                  re.hasChild = true
                  re.param = param
                } else if (row.level == 1) {
                  // // 进入第三级
                  re.category = re.doctName
                }
              })
              resolve(data)
            } else {
              resolve([])
            }
          })
          .catch(res => {
            console.log(res)
            setTimeout(() => {
              resolve([])
            }, 500)
          })
      })
    },
    refresh(){
      this.$refs.tstable.refresh()
    },
    sourceFn() {
      return {
        data: [
          {
            category: '01月',
            sameVal: 204894985.63,
            thisVal: 211276346.24,
            sameRate: 3.114454260741888,
            hasChild: true,
            parentId: 0
          },
          {
            category: '02月',
            sameVal: 107379557.38,
            thisVal: 211303606.24,
            sameRate: 96.78196799808788,
            hasChild: true,
            parentId: 0
          },
          {
            category: '03月',
            sameVal: 235135351.31,
            thisVal: 211330866.24,
            sameRate: -10.123737216619718,
            hasChild: true,
            parentId: 0
          },
          {
            category: '04月',
            sameVal: 276358662.54,
            thisVal: 211358126.24,
            sameRate: -23.52035420297052,
            hasChild: true,
            parentId: 0
          },
          {
            category: '05月',
            sameVal: 276698010.06,
            thisVal: 211385386.24,
            sameRate: -23.604298348888527,
            hasChild: true,
            parentId: 0
          },
          {
            category: '06月',
            sameVal: 284578286.78,
            thisVal: 211412646.24,
            sameRate: -25.710197839711647,
            hasChild: true,
            parentId: 0
          },
          {
            category: '07月',
            sameVal: 309202682.77,
            thisVal: 211439906.24,
            sameRate: -31.617699967603674,
            hasChild: true,
            parentId: 0
          }
        ],
  filtrate: [
          {
            title: '时间',
            startName: 'startTime',
            endName: 'endTime',
            width: '200px',
            type: 'datePicker',
            value: [ this.$utils.getTodayDate() , this.$utils.getTodayDate(),],
            source:{ type:"daterange"},
            event: this.refresh
          },
  ],
        dataType: 'json',
        url:'/2321',
        id: '8cffba',
        offPage: false,
        rownumbers: true,
        showOverflow: true,
        pageSize: 100,
        colModel: [
          {
            title: '月份',
            field: 'category',
            width: 120
          },
          {
            title: '本期',
            field: 'thisVal',
            align: 'right',
            helpDetail: '当前月份'
          },
          {
            title: '同期',
            field: 'sameVal',
            align: 'right',
            helpDetail: '去年同月份'
          },
          {
            title: '同比增长%',
            field: 'sameRate',
            type: 'html',
            align: 'right'
          }
        ]
      }
    }
  }
}
</script>
<style scoped>
</style>
